<?php
// +----------------------------------------------------------------------
// | 07FLYSAAS [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2025-2035 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------

namespace app\plat\logic;

use think\Db;
use think\Request;

/**
 * 用户公告=》逻辑层
 */
class TenNotifyUser extends PlatBase
{
    /**
     * 用户公告列表
     * @param array $where
     * @param bool $field
     * @param string $order
     * @param int|mixed $paginate
     * @return
     */
    public function getTenNotifyUserList($where = [], $field = 'a.*,n.create_user_id', $order = '', $paginate = DB_LIST_ROWS)
    {
        $this->modelTenNotifyUser->alias('a');
        $join = [
            [SYS_DB_PREFIX . 'ten_notify n', 'n.id = a.notify_id', 'LEFT'],
        ];
        $this->modelTenNotifyUser->join = $join;
        $list = $this->modelTenNotifyUser->getList($where, $field, $order, $paginate);
        foreach ($list as &$row) {
            $row['read_state_text'] = ($row['read_state'] == '1') ? '<span class="label">已读</span>' : '<span class="label-danger label">未读</span>';
            $row['create_user_name'] = $this->modelTenUser->getValue(['id' => $row['create_user_id']], 'realname');
        }
        return $list;
    }

    /**
     * 用户公告编辑
     * @param array $data
     * @return array
     */
    public function tenNotifyUserRead($data = [])
    {
        $map['id'] = ['in', $data['id']];
        $data = [
            'read_state' => 1,
            'read_time' => format_time(),
        ];
        $result = $this->modelTenNotifyUser->updateInfo($map, $data);

        $result && ten_log('编辑', '编辑用户公告已读，where：' . http_build_query($map));

        return $result ? [RESULT_SUCCESS, '编辑成功'] : [RESULT_ERROR, $this->modelTenNotifyUser->getError()];
    }

    /**
     * 用户公告删除
     * @param array $where
     * @return array
     */
    public function tenNotifyUserDel($where = [])
    {

        $result = $this->modelTenNotifyUser->deleteInfo($where, true);

        $result && ten_log('删除', '删除用户公告，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelTenNotifyUser->getError()];
    }

    /**
     * 用户公告信息
     * @param array $where
     * @param bool $field
     * @return
     */
    public function getTenNotifyUserInfo($where = [], $field = 'a.*,n.name,n.content,n.create_user_id')
    {
        $this->modelTenNotifyUser->alias('a');
        $join = [
            [SYS_DB_PREFIX . 'ten_notify n', 'n.id = a.notify_id', 'LEFT'],
        ];
        $this->modelTenNotifyUser->join = $join;
        $info = $this->modelTenNotifyUser->getInfo($where, $field);
        if (!empty($info)) {
            $info['create_user_name'] = $this->modelTenUser->getValue(['id' => $info['create_user_id']], 'realname');
        }
        return $info;
    }


    /**
     * 获取列表搜索条件
     */
    public function getWhere($data = [])
    {

        //定义当前登录id
        $where['a.rece_user_id'] = !empty($data['rece_user_id']) ? $data['rece_user_id'] : TEN_USER_ID;;

        //关键字查
        !empty($data['keywords']) && $where['n.name|n.content'] = ['like', '%' . $data['keywords'] . '%'];

        //创建时间
        if (!empty($data['create_time'])) {
            $range_date = getDayStartEndTime($data['create_time']);
            $where['a.create_time'] = ['between', [strtotime($range_date['begin']), strtotime($range_date['end'])]];
        }

        //是否已读
        if (isset($data['read_state'])) {
            if (!empty($data['read_state']) || is_numeric($data['read_state'])) {
                $where['a.read_state'] = ['=', '' . $data['read_state'] . ''];
            }
        }

        return $where;
    }

    /**
     * 获取排序条件
     */
    public function getOrderBy($data = [])
    {
        $order_by = "";
        //排序操作
        if (!empty($data['orderField'])) {
            $orderField = $data['orderField'];
            $orderDirection = $data['orderDirection'];
            switch ($orderField) {
                case 'create_time':
                    $order_by = "a.create_time $orderDirection";
                    break;
                default:
                    $order_by = "a.create_time desc";
                    break;
            }
        }
        return $order_by;
    }

}
